package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"LiquidMeasurement/lib"
	."LiquidMeasurement/commonControllers"
	"fmt"
)


type CommonController struct {
	beego.Controller
}

func (this *CommonController) Get() {
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplName = "index.tpl"
}

func (this *CommonController) Udpserver() {
	cmd := lib.ControllerCmd{}
	if err := this.ParseForm(&cmd); err != nil {
		fmt.Println("err",err)
	}
	valid := validation.Validation{}
	_, err := valid.Valid(&cmd)
	if err != nil {
		// handle error
		fmt.Println("error")
	}
	lib.LogJson(cmd)
	if cmd.OpCode ==1 {
		SetupUdpServer()
	//	commonControllers.UdpServerController.SetupUdpServer()
	}
	//out,_ := json.Marshal(cmd)
	//fmt.Println("cmd",string(out))

}

func (this *CommonController) UdpBroadcast() {
	cmd := lib.ControllerCmd{}
	if err := this.ParseForm(&cmd); err != nil {
		fmt.Println("err",err)
	}
	valid := validation.Validation{}
	_, err := valid.Valid(&cmd)
	if err != nil {
		// handle error
		fmt.Println("error")
	}
	lib.LogJson(cmd)
	if cmd.OpCode ==1 {
		err := SendInformBroadcast()
		if err != nil{
			cmd.RespCode = -1;
		}else {
			cmd.RespCode = 200;
		}
		//	commonControllers.UdpServerController.SetupUdpServer()
	}
	//out,_ := json.Marshal(cmd)
	//fmt.Println("cmd",string(out))

	this.Data["json"] = &cmd
	this.ServeJSON()
}



